<?php
/**
 * admin service file Created by PhpStorm.
 * User: wumengmeng
 * Date: 2019/09/03
 * Time: 15:20
 */

namespace Modules\Shop\Service;

use HiCommon\Repository\GoodsRepository;
use HiCommon\Repository\RepairstoreGoodsRepository;
use HiCommon\Repository\ShopRepository;

class ShopService
{
    /**
     * 获取分页数据
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_list($where)
    {
        $with['with'] = ['area_name'];
        $data = ShopRepository::get_list($where, $with);

        $data->appends($where);//添加链接参数
        $list = list_page_data($data, $where);
        $list['province'] = ShopRepository::get_open_province();
        $list['city'] = PublicService::getCityAraeList(['pid' => $where['province']]);//城市
        $list['area'] = PublicService::getCityAraeList(['pid' => $where['city']]);//县

        return $list;
    }

    /**
     * 获取门店详情
     */
    public function get_shop_detail($n_shop_id)
    {
        try {
            $map = ['id' => $n_shop_id];
            $arr_option['with'] = ['province_name', 'city_name', 'area_name', 'checkpoint_service_type', 'type'];
            $detail = ShopRepository::get_one_by_where($map, $arr_option);

            if ($detail) {
                $info = $detail->toarray();
                if (stripos($info['expenses'], '><tbody>')) {
                    $change = ' align="center" width="100%" style="line-height: 35px;"><tbody>';
                    $info['expenses'] = str_replace('><tbody>', $change, $info['expenses']);
                }
                if (stripos($info['expenses'], 'src="/')) {
                    $info['expenses'] = str_replace('src="/', 'src="https://xdminiapi.chxing.cn/', $info['expenses']);
                }
                return ['detail' => $info];
            }
            return ['detail' => []];
        } catch (\Exception $exception) {
            return hello_error('失败', [], $exception->getMessage());
        }
    }

    /**
     * 门店商品列表
     */
    public function admin_shop_goods_list(array $arr_input)
    {
        $arr_input = hi_array_del_null($arr_input);
        try {
            $arr_option['shop'] = shop_shop_id();//门店id
            $data = GoodsRepository::get_all($arr_input);

            $data->appends($arr_option);//添加链接参数
            $data = list_page_data($data, $arr_option);

            return hello_success('成功', $data);
        } catch (\Exception $exception) {
            return hello_error('失败', [], $exception->getMessage());
        }
    }

    /**
     * 门店服务商品列表
     */
    public function admin_shop_repair_list(array $arr_input)
    {
        $arr_input = hi_array_del_null($arr_input);
        try {
            $data = RepairstoreGoodsRepository::get_list($arr_input);

            $data->appends($arr_input);//添加链接参数
            $list = list_page_data($data, $arr_input);
            return $list;
        } catch (\Exception $exception) {
            return hello_error('失败', [], $exception->getMessage());
        }
    }

    /**
     * 检测手机号或门店名称是否存在
     */
    public function dataMonitoring(array $where)
    {
        $map[$where['type']] = $where['value'];
        $num = ShopRepository::data_monitoring($map, (int)$where['id']);

        if ($num > 0 && $where['type'] == 'name') {
            return hello_error('店铺名称已存在');
        }

        if ($num > 0 && $where['type'] == 'phone') {
            return hello_error('手机号已存在');
        }

        return hello_success('OK');
    }

}